//--------------------------------------------------------------------------------------------------------------------
// Show the comments by opening a pop up window
function ShowComments(location,queryString)
{
	var dialogPosition = 'width=700,height=500,scrollbars=yes,resizable=no,status=no'; 
	if(queryString != null)
	{
		location += '?' + queryString;
	}
	window.open(location,null,dialogPosition);
	return false;
}

//--------------------------------------------------------------------------------------------------------------------
// Get the number of days in a given month
function GetDaysInMonth(months)
{
	for (var i = 1; i <= months; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) 
		{
			this[i] = 30
		}
		if (i==2) 
		{
			this[i] = 29
		}
	} 
	return this;
}

//--------------------------------------------------------------------------------------------------------------------
//Strip the delimiters from the given number/string
function StripDelimiters(number, delimiter)
{
	var returnString = "";
	for (var i = 0; i < number.length; i++)
	{   
		var c = number.charAt(i);
		if (delimiter.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

//--------------------------------------------------------------------------------------------------------------------
//Check if the date entered by the user is in the valid format "mm/dd/yyyy"
function CheckforValidDate(dateEntered)
{
	var dateDelimiter			= "/";
	var minYear					= 1;
	var maxYear					= 2100;
	var daysInMonth				= GetDaysInMonth(12);
	var delimiterFirstIndex		= dateEntered.indexOf(dateDelimiter);
	var delimiterLastIndex		= dateEntered.indexOf(dateDelimiter,delimiterFirstIndex+1);
	var monthEntered			= dateEntered.substring(0,delimiterFirstIndex);
	var dayEntered				= dateEntered.substring(delimiterFirstIndex+1,delimiterLastIndex);
	var yearEntered				= dateEntered.substring(delimiterLastIndex+1);
	var tempYear				= yearEntered;
	
	if (dayEntered.charAt(0)=="0" && dayEntered.length>1) 
		dayEntered=dayEntered.substring(1);
	if (monthEntered.charAt(0)=="0" && monthEntered.length>1) 
		monthEntered=monthEntered.substring(1);
	for (var i = 1; i <= 3; i++) 
	{
		if (tempYear.charAt(0)=="0" && tempYear.length>1) 
			tempYear=tempYear.substring(1);
	}
	month=parseInt(monthEntered);
	day=parseInt(dayEntered);
	year=parseInt(tempYear);
	
	if (delimiterFirstIndex==-1 || delimiterLastIndex==-1)
	{
		alert("The date format should be : mm/dd/yyyy");
		return false;
	}
	if (monthEntered.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month.");
		return false;
	}

	if (yearEntered.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dateEntered.indexOf(dateDelimiter,delimiterLastIndex+1)!=-1 || CheckforValidInteger(StripDelimiters(dateEntered, dateDelimiter))==false)
	{
		alert("Please enter a valid date.");
		return false;
	}
	return true;
}

//--------------------------------------------------------------------------------------------------------------------
// Check if the given number is a valid integer
function CheckforValidInteger(number)
{
	for (var i = 0; i < number.length; i++)
	{   
		// Check that current character is number.
		var character = number.charAt(i);
		if (((character < "0") || (character > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

//--------------------------------------------------------------------------------------------------------------------
// Check if the number is a valid numeric value
function CheckforValidNumber(number)
{
	var numberValue = StripDelimiters(number,",");
	return(parseFloat(numberValue,10)==(numberValue*1));
}

//--------------------------------------------------------------------------------------------------------------------
// Validate the details entered by the user in the Annual Rate and Effective Date text boxes in the Salary adjustments grids
function ValidateSalaryAdjustment()
{
	// get the body Element tag;
	var bodyTag = document.getElementsByTagName("body")[0];
	// get the all the elements in the table salarygrid ;
	var tableElement = bodyTag.getElementsByTagName("table").item("salaryGrid");
	// get all the 'tr' elements
	var tableRow = tableElement.getElementsByTagName("tr");
	
	if (!tableRow) return;
	var returnValue=true;
	for (var i=0; i<tableRow.length;i++)
	{
		// check for the tr whose class = "DataGridEditRow"
		if (tableRow.item(i).className == "DataGridEditRow")
		{
			// get the annualrate / effectivedate and do validations;
			var annualRate =tableRow.item(i).getElementsByTagName("input")[0].value;
			// if the annualrate has $ symbol, remove the same and assign the value to the annualRate.
			if ((annualRate.length > 0) && (annualRate.substring(1,0) == '$'))
					annualRate = annualRate.substring(1);
			
			if (annualRate.length == 0)
			{
				alert('Please enter the Annual Rate');
				returnValue =false;
				break;
			}
			else if (annualRate <=0)
			{
				alert('Annual Rate must be greater than zero.');
				returnValue=false;
				break;
			}
			else if (annualRate >= 0 && annualRate < 1)
			{
				alert('Annual Rate must be a dollar amount.');
				returnValue=false;
				break;
			}
			else if (!CheckforValidNumber(annualRate))
			{
				alert('Annual Rate is invalid. Please re-enter.');
				returnValue = false;
				break;
			}
			var effectiveDate = tableRow.item(i).getElementsByTagName("input")[1].value;
			if (effectiveDate == '')
			{
				alert('Please Enter the EffectiveDate.');
				returnValue=false;
				break;
			}
			else if (!CheckforValidDate(effectiveDate)) 
			{
				returnValue=false;
				break;
			}
		}
	}
	return returnValue;
}

// --- EOF ---
